package controller;

import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class ExceptionHandler {
	
	private static FileWriter eLogWriter;
	private static ByteArrayOutputStream baos;
	
	public ExceptionHandler(){
		try{
		eLogWriter = new FileWriter("ErrorLog.log",true);
		baos = new ByteArrayOutputStream();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
		
	}
	
	
	public static void handleException(Exception e){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowtime = sdf.format(cal.getTime());
		baos.reset();

		try{
			eLogWriter.write(nowtime+'\n');
			e.printStackTrace(new PrintStream(baos));
			eLogWriter.write(baos.toString()+'\n');
			eLogWriter.flush();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
		
	}
	

}
